package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.vo.mall.MallOrderDetailListVO;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallIntegrationOrderDtoMapper;
import com.starlink.model.domain.MallIntegrationOrderDO;
import com.starlink.mapper.MallIntegrationOrderMapper;
import com.starlink.model.request.MallIntegrationOrderRequest;
import com.starlink.service.MallIntegrationOrderService;
import com.starlink.model.util.MallIntegrationOrderConvert;
import com.starlink.model.vo.MallIntegrationOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class MallIntegrationOrderServiceImpl extends ServiceImpl<MallIntegrationOrderMapper, MallIntegrationOrderDO> implements MallIntegrationOrderService {
    @Autowired
    private MallIntegrationOrderMapper mallIntegrationOrderMapper;

    @Override
    public MallIntegrationOrderVO getByKey(long id) {
        MallIntegrationOrderVO mallIntegrationOrderVO = MallIntegrationOrderDtoMapper.INSTANCE.do2VO(getById(id));
        return mallIntegrationOrderVO;
    }

    @Override
    public PageInfo<MallIntegrationOrderVO> pageQuery(Pageable pageable,MallIntegrationOrderRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallIntegrationOrderDO> list = mallIntegrationOrderMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallIntegrationOrderVO> mallIntegrationOrderVOList = MallIntegrationOrderConvert.buildMallIntegrationOrderVoList(list);
        return new PageInfo(mallIntegrationOrderVOList);
    }

    @Override
    public List<MallIntegrationOrderVO> listQuery(MallIntegrationOrderRequest request) {
        List<MallIntegrationOrderDO> list = mallIntegrationOrderMapper.selectDOList(MallIntegrationOrderDtoMapper.INSTANCE.request2DO(request));
        List<MallIntegrationOrderVO> mallIntegrationOrderVOS = MallIntegrationOrderConvert.buildMallIntegrationOrderVoList(list);
        return mallIntegrationOrderVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallIntegrationOrderRequest request) {
        request.setId(getId());
        save(MallIntegrationOrderDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallIntegrationOrderRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallIntegrationOrderDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallIntegrationOrderMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public MallOrderDetailListVO selectIntegrationOrderDetail(Long orderId) {
        return mallIntegrationOrderMapper.selectIntegrationOrderDetail(orderId);
    }
}
